
import axios from 'axios'

const baseURL = 'http://localhost:3000'
const service = axios.create({
  baseURL: baseURL,
  timeout: 1000
})
//请求拦截
service.interceptors.request.use(
  (config) => {
    let tk = sessionStorage['tk']
    config.headers.Authorization = tk
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)
//响应拦截
service.interceptors.response.use(
  (response) => {
    //  // 返回的status 以 2 开头会走这个
    return response.data
  },
  (error) => {
    // 响应拦截, status !== 200 会被这里拦截
    if (error.response.status === 401) {
      // ElMessage.error('token过期')
      // router.push({ path: '/' })
      // setTimeout(() => {
      //   window.location.reload()
      // }, 2000)
    }
    return Promise.reject(error)
  }
)

export default service
